<?php
	/**
	 * 订单控制器
	 */
	class OrderAction extends Action{

		//确认订单视图
		public function confirmOrder(){
			if($this->isPost()){
				$data = $_SESSION['cart'];
				$sumPrice = 0;
				foreach($data as $key=>$val){
					$data[$key]['number'] = $_POST['buynumberh'][$key];
					$data[$key]['totalPrice'] = number_format((int)$data[$key]['sprice']*(int)$_POST['buynumberh'][$key] , '2');
					$sumPrice +=(int)$data[$key]['sprice']*(int)$_POST['buynumberh'][$key];
				}
				$this->sumPrice = $sumPrice;
				$this->data=$data;
				$this->display();	
			}
		}

		//处理提交的订单数据
		public function addOrder(){
			if($this->isPost()){
				$tmpData = $_SESSION['cart'];
				$sumPrice = 0.00;
				foreach($tmpData as $key=>$val){
					$tmpData[$key]['number'] = $_POST['buynumberh'][$key];
					$tmpData[$key]['totalPrice'] = number_format((int)$tmpData[$key]['sprice']*(int)$_POST['buynumberh'][$key] , '2');
					$sumPrice +=(int)$tmpData[$key]['sprice']*(int)$_POST['buynumberh'][$key];
				}
				//计算运费
				if($this->_post('transporttype') == '1'){
					$transportPrice = 15;
				}else if($this->_post('transporttype') == '2'){
					$transportPrice = 20;
				}
				//计算是否是货到付款另收10元手续费
				if($this->_post('buytype') == '1'){
					$payPrice = 10;
				}else{
					$payPrice = 0;
				}
				$sumPrice = $sumPrice + $transportPrice + $payPrice;
				foreach($_SESSION['cart'] as $val){
					$order_list[]= array(
						'gid' => $val['gid'],
						'quantity' => $val['number'],
						'subtotal' => $val['totalPrice'],
						'mark' => $val['color'] . '&nbsp' . $val['size'],
						);	 
				}
				$data = array(
					'number' => "OSA".time().mt_rand(0,999),
					'consignee' => $this->_post('username'),
					'address' => $this->_post('area1').$this->_post('area2').$this->_post('area3').$this->_post('detaddress'),
					'total'=> $sumPrice,
					'status' => 1,
					'remark' => $this->_post('usermsg'),
					'uid' => $_SESSION['uid'],
					'time' => time(),
					'transporttype' =>$this->_post('transporttype'),
					'paytype' =>$this->_post('buytype'),
					'order_list' => $order_list
					);
				$db = D('OrderRelation');
				$oid = $db->relation(true)->add($data);
				if($oid){
					$_SESSION['cart'] = NULL;
					$_SESSION['oid'] = $oid;
					$this->success('订单提交成功!', U('showOrder',array('id' => $oid)));
				}else{
					$this->error('订单提交失败,请重试！');
				}
			}
		}

		//订单提交成功后显示订单信息
		public function showOrder(){
			if($this->_get('id')){
				$oid = $this->_get('id','intval');
				$db = D('OrderRelation');
				$orderData = $db->relation(true)->field('number,total,paytype,transporttype')->where(array('id'=>$oid))->find();
				$this->orderData = $orderData;
				$this->display();
			}
		}

	}
?>